package com.itstu.gulimall.order.service.impl;

import com.itstu.common.to.mq.SeckillOrderTo;
import com.itstu.gulimall.order.entity.OrderItemEntity;
import com.itstu.gulimall.order.feign.WaraFeignService;
import com.itstu.gulimall.order.service.OrderItemService;
import com.itstu.gulimall.order.vo.OrderSubmitVo;
import com.itstu.gulimall.order.vo.SubmitOrderResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itstu.common.utils.PageUtils;
import com.itstu.common.utils.Query;

import com.itstu.gulimall.order.dao.OrderDao;
import com.itstu.gulimall.order.entity.OrderEntity;
import com.itstu.gulimall.order.service.OrderService;
import org.springframework.transaction.annotation.Transactional;


@Service("orderService")
public class OrderServiceImpl extends ServiceImpl<OrderDao, OrderEntity> implements OrderService {
    @Autowired
    WaraFeignService waraFeignService;
    @Autowired
    OrderItemService orderItemService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderEntity> page = this.page(
                new Query<OrderEntity>().getPage(params),
                new QueryWrapper<OrderEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
//    @GlobalTransactional
    public SubmitOrderResponseVo submitOrder(OrderSubmitVo orderSubmitVo) {
        orderSubmitVo.setName("小明");
        System.out.println("第一步订单验证。。。。。");
        System.out.println("验证成功，即将对库存操作");
        OrderEntity orderEntity = new OrderEntity();
        String s = UUID.randomUUID().toString().replaceAll("-", "");
        System.out.println("生成的订单号是。。。"+s);
        orderEntity.setOrderSn(s);
        orderEntity.setCommentTime(new Date());
        baseMapper.insert(orderEntity);
        waraFeignService.orderTask();
        System.out.println("库存操作结束，进行下一步工作");
        SubmitOrderResponseVo submitOrderResponseVo = new SubmitOrderResponseVo();
        submitOrderResponseVo.setAddr("北京");
//        int i = 1/0;
        return submitOrderResponseVo;
    }

    @Override
    public void creatSeckillOrder(SeckillOrderTo entity) {
        System.out.println("调用service方法保存订单");
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setOrderSn(entity.getOrderSn());
        orderEntity.setMemberId(entity.getMemberId());
        BigDecimal multiply = entity.getSeckillPrice().multiply(new BigDecimal("" + entity.getNum()));
        orderEntity.setPayAmount(multiply);
        this.save(orderEntity);
        OrderItemEntity orderItemEntity = new OrderItemEntity();
        orderItemEntity.setOrderSn(entity.getOrderSn());
        orderItemEntity.setRealAmount(multiply);
        orderItemEntity.setSkuQuantity(entity.getNum());
        // TODO 其他项调用远程服务进行保存
        orderItemService.save(orderItemEntity);
    }

}